package com.lanou3g.k12.utils;

import com.github.pagehelper.Page;

import java.util.HashMap;

public class K12ResultUtils {

    private static final int DEFAULT_SUCCESS_CODE = 200;
    private static final String DEFAULT_SUCCESS_MESSAGE = "ok";
    private static final String DEFAULT_ERROR_MESSAGE = "error";

    public static K12Result ok(Object data) {
        K12Result k12Result = ok();
        k12Result.setData(data);
        return k12Result;
    }

    public static K12Result ok(Page page) {
        K12Result k12Result = ok();
        k12Result.setData(page.getResult());

        k12Result.setPaging(new HashMap<String, Object>());
        k12Result.getPaging().put("page", page.getPageNum());
        k12Result.getPaging().put("size", page.getPageSize());
        k12Result.getPaging().put("total", page.getTotal());
        return k12Result;
    }

    public static K12Result ok() {
        K12Result k12Result = new K12Result();
        k12Result.setCode(DEFAULT_SUCCESS_CODE);
        k12Result.setMessage(DEFAULT_SUCCESS_MESSAGE);
        return k12Result;
    }

    public static K12Result error(int code) {
        K12Result k12Result = new K12Result();
        k12Result.setCode(code);
        k12Result.setMessage(DEFAULT_ERROR_MESSAGE);
        return k12Result;
    }

    public static K12Result error(int code, String message) {
        K12Result k12Result = new K12Result();
        k12Result.setCode(code);
        k12Result.setMessage(message);
        return k12Result;
    }

}
